(function($) {
    
	$(function() {
		
		$('#wp-solr-action').change( function() {
			toggle('load', $('#wp-solr-load-types'), this);
			toggle('delete', $('#wp-solr-delete-types'), this);
		});

		$('#wp-solr-action-form').submit( function(event) {
			
			event.preventDefault();
			
			if ( $('#wp-solr-action').val() !== '' ) {
				
				$('#wp-solr-loading').show();
				$('#submit').attr('disabled', 'disabled');
				
				var params = ('&action=ajax_action_response&security={0}').replace('{0}', wp_solr_nonce);
				var data = $("#wp-solr-action-form").serialize() + params;
				
				jQuery.post(ajaxurl, data, function(response) {
					$( "#wp-solr-result" ).empty().append( response );
				})
				.error(function() { $('#wp-solr-loading').hide(); $('#submit').removeAttr('disabled'); })
				.complete(function() { $('#wp-solr-loading').hide(); $('#submit').removeAttr('disabled'); });

			}
			
			return false;
		
		});
		
		function toggle(needle, el, select) {
			if ( $(select).val() === needle ) {
				el.show();
			} else {
				el.hide();
			}
		}
		
	});
	
})(jQuery);